package com.dsh.admin.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dsh.admin.entity.ShopUser;
import com.dsh.admin.manager.ShopUserMng;

@Controller
@RequestMapping("/login")
public class LoginAct {
	/**
	 * 进入登陆页面
	 * @return
	 */
	@RequestMapping("/toLoginPage.do")
	public String toLoginPage(){
		return "bms/admin/login";
	}
	
	/**
	 * 登陆验证
	 * @return
	 */
	@RequestMapping("/login.do")
	public String login(HttpServletRequest request,HttpServletResponse response,ShopUser bean){
		
		if(!StringUtils.isEmpty(bean.getUsername())&&!StringUtils.isEmpty(bean.getPassword())){
			bean = shopUserMng.checkUsernameAndPwd(bean);
			if(bean!=null){
				//添加session
				HttpSession session = request.getSession();
				session.setAttribute("user", bean);
				return "redirect:/login/main.do";
			}
		}
		return "bms/admin/login";
	}
	
	
	@RequestMapping("/main.do")
	public String toMain(){
		return "bms/admin/main";
	}
	
	
	/**
	 * 加载主页顶部
	 * @return
	 */
	@RequestMapping("/toTop.do")
	public String toTopPage(){
		return "bms/admin/top";
	}
	
	/**
	 * 加载主页中部
	 * @return
	 */
	@RequestMapping("/toCenter.do")
	public String toCenterPage(){
		return "bms/admin/center";
	}
	/**
	 * 加载主页低部
	 * @return
	 */
	@RequestMapping("/toDown.do")
	public String toDownPage(){
		return "bms/admin/down";
	}
	
	/**
	 * 加载主页左部
	 * @return
	 */
	@RequestMapping("/toLeft.do")
	public String toLeftPage(){
		return "bms/admin/left";
	}
	/**
	 * 加载主页右部
	 * @return
	 */
	@RequestMapping("/toRight.do")
	public String toRightPage(){
		return "bms/admin/right";
	}
	
	
	@RequestMapping("/toTest.do")
	public String toTestPage(){
		return "cms/test";
	}
	
	
	public static ShopUser getLoginUser(HttpServletRequest request){
		HttpSession session = request.getSession();
		ShopUser user = (ShopUser)session.getAttribute("user");
		return user;
	}
	
	private ShopUserMng shopUserMng;

	@Resource(name="shopUserMngImpl")
	public void setShopUserMng(ShopUserMng shopUserMng) {
		this.shopUserMng = shopUserMng;
	}
	
	
	
}
